package com.fwtai.service.api;

import com.fwtai.bean.PageFormData;
import com.fwtai.config.LocalUserId;
import com.fwtai.service.AsyncService;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 收藏知识
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021年7月22日 11:43:17
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class ApiStatisticsService{

    @Resource
    private AsyncService asyncService;

    public String addHome(final PageFormData formData){
        final String p_device_id = "device_id";
        final String p_platform = "platform";
        final String p_model = "model";
        final String p_tabs = "tabs";
        final String validate = ToolClient.validateField(formData,p_device_id);
        if(validate != null){
          return validate;
        }
        if(formData.getString(p_device_id).length() > 40){
            return ToolClient.createJsonFail("设备id长度有误");
        }
        if(formData.getString(p_platform).length() > 20){
            return ToolClient.createJsonFail("客户端平台有误,值域为：ios、android、mac、windows、linux");
        }
        if(formData.getString(p_model).length() > 64){
            return ToolClient.createJsonFail("设备型号有误");
        }
        final String tabs = formData.getString(p_tabs);
        if(tabs == null || tabs.length() <= 0){
            formData.put("tabs","1");
        }else{
            try {
                formData.put("tabs",Integer.parseInt(tabs));
            } catch (final Exception e) {
                formData.put("tabs","1");
            }
        }
        formData.put("kid",new IdBuilder().getId121L());
        final String loginId = LocalUserId.get();
        if(loginId != null){
            formData.put("loginId",loginId);
        }
        asyncService.addHome(formData);
        return ToolClient.executeRows(1);
    }

  public String addConsultRecord(final PageFormData formData){
    final String p_lawyer_id = "lawyer_id";
    final String validate = ToolClient.validateField(formData,p_lawyer_id);
    if(validate != null){
      return validate;
    }
    final String len_lawyer_id = formData.getString(p_lawyer_id);
    if(len_lawyer_id != null && len_lawyer_id.length() > 19){
      return ToolClient.createJsonFail("被咨询的律师kid字数太多");
    }
    final Long v_lawyer_id = formData.getLong(p_lawyer_id);
    if(v_lawyer_id == null){
      return ToolClient.createJsonFail("被咨询的律师kid不是有效的数据格式");
    }
    formData.put("kid",new IdBuilder().getId121L());
    final String loginId = LocalUserId.get();
    if(loginId != null){
      formData.put("user_id",loginId);
    }
    asyncService.addConsultRecord(formData);
    return ToolClient.executeRows(1);
  }
}